package com.buka.recipe.security.strategy;

import com.alibaba.fastjson2.JSON;
import com.buka.recipe.common.result.Result;
import com.buka.recipe.common.result.ResultCode;
import com.buka.recipe.common.result.ResultGenerator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.InvalidSessionStrategy;

import java.io.IOException;

/**
 * @author admin
 */
public class JsonInvalidSessionStrategy implements InvalidSessionStrategy {

    @Override
    public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 会话已过期
        Result<?> result = ResultGenerator.genFailResult(ResultCode.UNAUTHORIZED.code(), "会话已失效, 请重新登录.");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(result));
    }

}
